package com.asa.ui.I;

import java.awt.EventQueue;
import java.util.Arrays;

import com.asa.ui.I.AlgoFrame;
import com.asa.ui.I.sort.InsertionSortData;
import com.asa.ui.I.sort.SelectionSortData;
import com.asa.ui.frame.AlgoVisHelper;


public class AlgoInsertionSortVisualizer {
	private AlgoInsertionSortFrame frame;
	private InsertionSortData data;
	public AlgoInsertionSortVisualizer(final int screenWidth,final int screenHeight,int N){
		
		data = new InsertionSortData(N, screenHeight);
		
		EventQueue.invokeLater(new Runnable() {//用java事件分发（开了个线程）的方法来创建对话框
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				frame=new AlgoInsertionSortFrame("排序",screenWidth,screenHeight,data);
				
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						runmiage();
					}

					
				}).start(); ;
				
			}
		});
		
		
	}
	//播放动画
	public void runmiage(){
		
			setData(0, -1);

			for (int i = 0; i < data.N(); i++) {
				setData(i, i);
				for (int j = i; j>0&&data.get(j)<data.get(j-1); j--) {
					data.swap(j, j-1);
					setData(i+1, j-1);
				}
				
			}
			
			
			setData(data.N(), -1);

		
	}

	private void setData(int orderedIndex,int currentIndex) {
		data.orderedIndex = orderedIndex;
		data.currentIndex = currentIndex;
		

		frame.render(data);
		AlgoVisHelper.pause(40);
	}
	
	
}